package com.hoshiibuy.spu.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.spu.entity.BaseSpu;
import com.hoshiibuy.spu.param.BaseSpuPageParam;
import com.hoshiibuy.spu.param.BaseSpuUpdateParam;
import com.hoshiibuy.spu.service.IBaseSpuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * SPU基本资料controller类
 *
 * @author : yangcheng
 * @date : 2019/4/30 14:23
 * @mail : maiboyc@163.com
 **/
@RestController
@RequestMapping("/baseSpu")
@Api(value = "SPU基本资料管理", tags = {"SPU基本资料操作接口"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class BaseSpuController extends BaseController {

    // SPU基本资料service
    @Autowired
    private IBaseSpuService baseSpuService;

    /**
     * 根据ID查询spu基本资料信息
     * @param id spu基本资料ID
     * @return Response
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询", notes = "根据ID查询spu基本资料信息", consumes = MediaType.ALL_VALUE)
//    @ApiImplicitParam(name = "id", value = "spu基本资料主键ID", required = true, dataType = "String", example = "3187243209717710848", paramType = "query")
    public Response get(@PathVariable("id") Long id) {
        return Response.data(baseSpuService.get(id));
    }

    /**
     * 根据名称查询spu基本资料信息
     * @param name  租户spu标题
     * @return Response
     */
    @GetMapping("/find/{name}")
    @ApiOperation(value = "根据名称查询", notes = "根据名称查询spu基本资料信息", consumes = MediaType.ALL_VALUE)
//    @ApiImplicitParam(name = "name", value = "spu基本资料标题", required = true, dataType = "String", example = "衬衫", paramType = "query")
    public Response findSpuByName(@PathVariable("name") String name) {
        Wrapper wrapper = Wrappers.<BaseSpu>query().lambda().like(BaseSpu::getTitle, name);
        return Response.data(baseSpuService.getOne(wrapper));
    }

    /**
     * 分页查询spu基本资料信息（get请求，以具体值接收参数）
     * @param pageIndex 当前页
     * @param pageSize  每页大小
     * @param title spu标题
     * @param subTitle spu副标题
     * @param saleUnit 销售单位
     * @param packetUnit 包装单位
     * @return Response
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询spu基本资料信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", required = true, dataType = "Long", example = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", required = true, dataType = "Long", example = "10", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "spu标题", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "subTitle", value = "spu副标题", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "saleUnit", value = "销售单位", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "packetUnit", value = "包装单位", dataType = "String", paramType = "query")
    })
    public Response list(Long pageIndex, Long pageSize, String title, String subTitle, String saleUnit, String packetUnit) {
        BaseSpu entity = new BaseSpu();
        entity.setPageIndex(pageIndex);
        entity.setPageSize(pageSize);
        entity.setTitle(title);
        entity.setSubTitle(subTitle);
        entity.setSaleUnit(saleUnit);
        entity.setPacketUnit(packetUnit);
        return Response.data(baseSpuService.List(entity));
    }

    /**
     * 分页查询spu基本资料信息（post请求，以对象接收参数）
     * @param param 查询参数实体
     * @return Response
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询spu基本资料信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response list(@RequestBody BaseSpuPageParam param) {
        BaseSpu entity = new BaseSpu();
        BeanUtils.copyProperties(param, entity);
        return Response.data(baseSpuService.List(entity));
    }

    /**
     * 新增spu基本资料
     * @param entity 保存参数实体
     * @return Response
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增spu基本资料", notes = "新增spu基本资料", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response saveSpu(@RequestBody @Valid BaseSpu entity, BindingResult result) {
        Response rs = new Response();
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(objectMapper.writeValueAsString(handleValidMsg(result)));
            } else {// 验证通过
                rs = Response.data(baseSpuService.save(entity));
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 修改spu基本资料
     * @param param 修改参数实体
     * @return Response
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改spu基本资料", notes = "修改spu基本资料", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response updateSpuById(@RequestBody @Valid BaseSpuUpdateParam param, BindingResult result) {
        Response rs = new Response();
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(objectMapper.writeValueAsString(handleValidMsg(result)));
            } else {// 验证通过
                BaseSpu entity = new BaseSpu();
                BeanUtils.copyProperties(param, entity);
                rs = Response.data(baseSpuService.update(entity));
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 根据ID删除spu基本资料信息
     * @param id spu基本资料ID
     * @return Response
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除spu基本资料信息", consumes = MediaType.ALL_VALUE)
//    @ApiImplicitParam(name = "id", value = "spu基本资料主键ID", required = true, dataType = "String", example = "3187243209717710848", paramType = "query")
    public Response deleteById(@PathVariable("id") Long id) {
        return Response.data(baseSpuService.delete(id));
    }
}  